package com.guinea.system.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.guinea.core.utils.StringUtils;
import com.guinea.system.entity.UserRoleRel;
import com.guinea.system.form.UserRoleRelForm;
import com.guinea.core.service.EntityService;
import com.guinea.core.web.StrutsEntityAction;
import com.guinea.system.service.UserRoleRelService;
import com.guinea.system.entity.User;
import com.guinea.system.service.UserService;
import com.guinea.system.entity.Role;
import com.guinea.system.service.RoleService;

public class UserRoleRelAction extends StrutsEntityAction {
	private UserRoleRelService userRoleRelService;

	public EntityService getEntityService() {
		return userRoleRelService;
	}

	public UserRoleRelService getUserRoleRelService() {
		return userRoleRelService;
	}

	public void setUserRoleRelService(UserRoleRelService userRoleRelService) {
		this.userRoleRelService = userRoleRelService;
	}
	
	private UserService userService;

	private User user;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ActionForward cannotMissUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		user = null;

		String userIdString = request.getParameter("userId");
		if (StringUtils.isEmpty(userIdString)) {
			saveError(request, "parameter.missing");
			return mapping.findForward(LIST);
		}
		Long userId = new Long(userIdString);

		user = getUserService().getUser(userId);
		if (user == null) {
			saveError(request, "entity.missing");
			return mapping.findForward(LIST);
		}
		return null;
	}
	
	private RoleService roleService;

	private Role role;

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public ActionForward cannotMissRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		role = null;

		String roleIdString = request.getParameter("roleId");
		if (StringUtils.isEmpty(roleIdString)) {
			saveError(request, "parameter.missing");
			return mapping.findForward(LIST);
		}
		Long roleId = new Long(roleIdString);

		role = getRoleService().getRole(roleId);
		if (role == null) {
			saveError(request, "entity.missing");
			return mapping.findForward(LIST);
		}
		return null;
	}

	/**
	 * 显示Form表单时的回调函数.为Form对象添加更多属性,在子类重载.
	 */
	public void editRender(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserRoleRelForm userRoleRelForm = (UserRoleRelForm) form;
		UserRoleRel userRoleRel = (UserRoleRel) entity;
		
		if (entity != null) 
			userRoleRelForm.setUserId(userRoleRel.getUser().getId());
		request.setAttribute("allUser", getUserService().get());
		if (entity != null) 
			userRoleRelForm.setRoleId(userRoleRel.getRole().getId());
		request.setAttribute("allRole", getRoleService().get());
	}

	public ActionForward saveValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward actionForward = super.saveValidator(mapping, form,
				request, response);
		
		if (actionForward == null)
			actionForward = cannotMissUser(mapping, form, request, response);
		if (actionForward == null)
			actionForward = cannotMissRole(mapping, form, request, response);
		
		if(entity == null && getUserRoleRelService().getByUserRole(user.getId(), role.getId()).size() > 0){
			saveError(request, "entity.duplicated");
			return mapping.findForward(LIST);
		}

		return actionForward;
	}

	/**
	 * 保存业务对象的函数.
	 */
	protected void doSaveEntity(HttpServletRequest request) {
		UserRoleRel userRoleRel = (UserRoleRel) entity;
		
		userRoleRel.setUser(user);
		userRoleRel.setRole(role);
		
		entity = userRoleRel;
		super.doSaveEntity(request);
	}

}
